import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:music_app/player/bottom_player_bar.dart';

class BottomPlayerOverlayController extends GetxController {
  OverlayEntry? _entry;
  bool _inserted = false;

  final isVisible = false.obs;

  final bottomOffset = 0.0.obs;

  /// 初始化 overlay（只执行一次）
  void initOverlay(BuildContext context) {
    if (_inserted) return;

    _entry = OverlayEntry(
      builder: (_) => Obx(() {
        return isVisible.value
            ? Positioned(
                left: 0,
                right: 0,
                bottom: bottomOffset.value,
                child: const BottomPlayerBar(),
              )
            : const SizedBox.shrink();
      }),
    );

    final overlay = Overlay.of(Get.overlayContext!);
    if (_entry != null) {
      overlay.insert(_entry!);
      _inserted = true;
    }
  }

  /// 显示播放栏
  void show(double bottom) {
    bottomOffset.value = bottom;
    isVisible.value = true;
  }

  void setBottom(double bottom) {
    bottomOffset.value = bottom;
  }

  /// 隐藏播放栏
  void hide() {
    isVisible.value = false;
  }

  /// 可选：销毁 overlay
  void removeOverlay() {
    _entry?.remove();
    _inserted = false;
  }
}
